// pages/clockCheck/clockCheck.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    members: [],
    index: 0,
    month: '',
    day: '01',
    dayIndex: 0,
    memberIndex:0,
    memberArray: [],//出差人员
    members: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let date = util.getNowDate();
    that.setData({
      nowDate: date,
      startTime: date,
      endTime: date,
      riginDate: '2020-01-01',
      scrollHeight:app.getScrollHeight(107)
    })
    that.getMembers();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 查看打卡详情
   */
  toDetail: function (e) {
    console.log('toDetail>>', e)
    let cardDate = e.currentTarget.dataset.date
    let uid = e.currentTarget.dataset.uid
    wx.navigateTo({
      url: '../clockDetail/clockDetail?cardDate=' + cardDate + '&uid=' + uid + '&type=2',
    })
  },
  /**
   * 获取补卡申请列表
   */
  getList: function(){
    let that = this
    lbbService.getRepairCard({
      cardStartTime: that.data.startTime,
      cardEndTime: that.data.endTime,
      uid: that.data.members[that.data.memberIndex].masterUid,
    }, function (res) {
      that.setData({
        applyList: res.data
      })
    })
  },
  /**
   * 同意
   */
  agree: function(e){
    let that = this
    let id = e.currentTarget.dataset.id
    lbbService.checkApply({ isOK: 2, repairId: id},function(res){
      wx.showToast({
        title: '已同意！',
      })
      setTimeout(function(){
        that.getList()
      },1500)
    })
  },
  /**
   * 不同意
   */
  disagree: function (e) {
    let that = this
    let id = e.currentTarget.dataset.id
    lbbService.checkApply({ isOK: 1, repairId: id }, function (res) {
      wx.showToast({
        title: '已拒绝！',
      })
      setTimeout(function () {
        that.getList()
      }, 1500)
    })
  },
  /**
   * 获取出差人员
   */
  getMembers: function() {
    let that = this
    lbbService.getMembers({}, function(res) {
      let members = res.data
      let item = {}
      item.masterName = '全部'
      item.masterUid = ''
      members.unshift(item);
      that.setData({
        members: members
      })
      that.setName();
    })
  },
  setName: function() {
    let that = this
    let members = that.data.members
    let array = []
    members.forEach(item => {
      array.push(item.masterName)
    })
    that.setData({
      memberArray: array
    })
    that.getList();
  },
  /**
   * 选择出差人员
   */
  memberChange(e){
    let that = this;
    let memberIndex = e.detail.value;
    that.setData({
      memberIndex:memberIndex
    })
    that.getList();
  },

  /**
   * 选择时间
   */
  startTimeChange: function(e) {
    let that = this;
    that.setData({
      startTime:e.detail.value
    })
    that.getList();
  },

  /**
   * 选择时间
   */
  endTimeChange: function(e) {
    let that = this;
    that.setData({
      endTime:e.detail.value
    })
    that.getList();
  },
})